package com.wdl.modules.myhouse.controller;

import com.alibaba.fastjson.JSONObject;
import com.wdl.modules.myhouse.common.Code;
import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.security.entity.UserInfo;
import com.wdl.modules.myhouse.service.AnimesService;
import com.wdl.modules.myhouse.utils.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 动漫表
 *
 * @author lucain sunlightcs@gmail.com
 * @since 1.0.0 2022-09-10
 */
@RestController
@RequestMapping("/anime")
@Api(tags="动漫表")
public class AnimesController {
   
   
    @Autowired
    private AnimesService animesService;
   


    @GetMapping("/getMyAnimes")
    @ApiOperation(value = "根据用户id(ID从springSecurity中拿取),查找动漫", tags = "根据用户id查找动漫")
    public Result<List<JSONObject>> getMyGames(){
        Integer userId = null;
        try {
            UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            userId = user.getId();
            if (userId==null){
                throw new NullPointerException();
            }
        } catch (NullPointerException e) {
            return new Result<List<JSONObject> >().error(Code.UNAUTHORIZED.getCode(),"查询不到当前用户信息");
        }
        Map<Integer, Object> animeMap = animesService.getAnimeByUserId(userId);
        Set<Integer> keys = animeMap.keySet();
        ArrayList<JSONObject> list = new ArrayList<>();
        for(Integer key: keys){
            Object o = animeMap.get(key);
            list.add(JSONObject.parseObject(JsonUtil.obj2String(o)));
        }
        return new Result<List<JSONObject> >().ok().setData(list);
    }

}